﻿using System;
using System.Data;
using System.Data.Common;
using System.Text;
using System.Collections.Generic;
using System.Web;
using System.Web.Profile;
using System.Web.Security;
using SecurityLib;

/// <summary>
/// Сводное описание для CommerceLibAccess
/// </summary>
public class CommerceLibAccess
{
    /// <summary>
    /// Wraps order detail data
    /// </summary>
    public class CommerceLibOrderDetailInfo
    {
        public int OrderID;
        public int ProductID;
        public string ProductName;
        public int Quantity;
        public double UnitCost;
        public string ItemAsString;
        public double Subtotal
        {
            get
            {
                return Quantity * UnitCost;
            }
        }
        public CommerceLibOrderDetailInfo(DataRow orderDetailRow)
        {
            OrderID = Int32.Parse(orderDetailRow["id_order"].ToString());
            ProductID = Int32.Parse(orderDetailRow["id_present"].ToString());
            ProductName = orderDetailRow["name_p"].ToString();
            Quantity = Int32.Parse(orderDetailRow["amount"].ToString());
            UnitCost = Double.Parse(orderDetailRow["unit_cost"].ToString());
            // set info property
            Refresh();
        }
        public void Refresh()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(Quantity.ToString());
            sb.Append(" ");
            sb.Append(ProductName);
            sb.Append(", $");
            sb.Append(UnitCost.ToString());
            sb.Append(" each, total cost $");
            sb.Append(Subtotal.ToString());
            ItemAsString = sb.ToString();
        }
    }

    /// <summary>
    /// Wraps order data
    /// </summary>
    public class CommerceLibOrderInfo
    {
        public int OrderID;
        public string DateCreated;
        public string DateShipped;
        public string Comments;
        public int Status;
        public string AuthCode;
        public string Reference;
        public MembershipUser Customer;
        public ProfileCommon CustomerProfile;
        public SecureCard CreditCard;
        public double TotalCost;
        public string OrderAsString;
        public string CustomerAddressAsString;
        public List<CommerceLibOrderDetailInfo> OrderDetails;
        public CommerceLibOrderInfo(DataRow orderRow)
        {
            OrderID = Int32.Parse(orderRow["id_order"].ToString());
            DateCreated = orderRow["date_created"].ToString();
            DateShipped = orderRow["date_shipped"].ToString();
            Comments = orderRow["comments"].ToString();
            Status = Int32.Parse(orderRow["status"].ToString());
            AuthCode = orderRow["auth_code"].ToString();
            Reference = orderRow["reference"].ToString();
            Customer = Membership.GetUser(
            new Guid(orderRow["id_customer"].ToString()));
            CustomerProfile =
            (HttpContext.Current.Profile as ProfileCommon)
            .GetProfile(Customer.UserName);
            CreditCard = new SecureCard(CustomerProfile.CreditCard);
            OrderDetails =
            CommerceLibAccess.GetOrderDetails(
            orderRow["id_order"].ToString());
            // set info properties
            Refresh();
        }
        public void Refresh()
        {
            // calculate total cost and set data
            StringBuilder sb = new StringBuilder();
            TotalCost = 0.0;
            foreach (CommerceLibOrderDetailInfo item in OrderDetails)
            {
                sb.AppendLine(item.ItemAsString);
                TotalCost += item.Subtotal;
            }
            sb.AppendLine();
            sb.Append("Total order cost: $");
            sb.Append(TotalCost.ToString());
            OrderAsString = sb.ToString();
            // get customer address string
            sb = new StringBuilder();
            sb.AppendLine(Customer.UserName);
            sb.AppendLine(CustomerProfile.Address1);
            if (CustomerProfile.Address2 != "")
            {
                sb.AppendLine(CustomerProfile.Address2);
            }
            sb.AppendLine(CustomerProfile.City);
            sb.AppendLine(CustomerProfile.Region);
            sb.AppendLine(CustomerProfile.PostalCode);
            sb.AppendLine(CustomerProfile.Country);
            CustomerAddressAsString = sb.ToString();
        }
    }

    public CommerceLibAccess()
    {
        //
        // TODO: добавьте логику конструктора
        //
    }

    public static List<CommerceLibOrderDetailInfo> GetOrderDetails(string orderId)
    {
        // use existing method for DataTable
        DataTable orderDetailsData = OrdersAccess.GetDetails(orderId);
        // create List<>
        List<CommerceLibOrderDetailInfo> orderDetails =
        new List<CommerceLibOrderDetailInfo>(
        orderDetailsData.Rows.Count);
        foreach (DataRow orderDetail in orderDetailsData.Rows)
        {
            orderDetails.Add(
            new CommerceLibOrderDetailInfo(orderDetail));
        }
        return orderDetails;
    }

    public static CommerceLibOrderInfo GetOrder(string orderID)
    {
        // get a configured DbCommand object
        DbCommand comm = GenericDataAccess.CreateCommand();
        // set the stored procedure name
        comm.CommandText = "CommerceLibOrderGetInfo";
        // create a new parameter
        DbParameter param = comm.CreateParameter();
        param.ParameterName = "@id_order";
        param.Value = orderID;
        param.DbType = DbType.Int32;
        comm.Parameters.Add(param);
        // obtain the results
        DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
        DataRow orderRow = table.Rows[0];
        // save the results into an CommerceLibOrderInfo object
        CommerceLibOrderInfo orderInfo =
        new CommerceLibOrderInfo(orderRow);
        return orderInfo;
    }
}